// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import zombie.ai.State;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.characters.CharacterTimedActions.BaseAction;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.debug.DebugLog;
import zombie.network.GameClient;
import zombie.network.GameServer;

public final class PlayerHitReactionState extends State {
    private static final PlayerHitReactionState _instance = new PlayerHitReactionState();

    public static PlayerHitReactionState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        owner.setIgnoreMovement(true);
        if (!owner.getCharacterActions().isEmpty()) {
            ((BaseAction)owner.getCharacterActions().get(0)).forceStop();
        }

        owner.setIsAiming(false);
    }

    @Override
    public void execute(IsoGameCharacter owner) {
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        owner.setIgnoreMovement(false);
        owner.setHitReaction("");
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
        if (owner.getAttackedBy() != null && owner.getAttackedBy() instanceof IsoZombie) {
            if (event.m_EventName.equalsIgnoreCase("PushAwayZombie")) {
                owner.getAttackedBy().setHitForce(0.03F);
                ((IsoZombie)owner.getAttackedBy()).setPlayerAttackPosition(null);
                ((IsoZombie)owner.getAttackedBy()).setStaggerBack(true);
            }

            if (event.m_EventName.equalsIgnoreCase("Defend")) {
                owner.getAttackedBy().setHitReaction("BiteDefended");
                if (GameClient.bClient) {
                    GameClient.sendHitCharacter(owner.getAttackedBy(), owner, null, 0.0F, false, 1.0F, false, false, false);
                }
            }

            if (event.m_EventName.equalsIgnoreCase("DeathSound")) {
                if (owner.isPlayingDeathSound()) {
                    return;
                }

                owner.setPlayingDeathSound(true);
                String string = "Male";
                if (owner.isFemale()) {
                    string = "Female";
                }

                string = string + "BeingEatenDeath";
                owner.playSound(string);
            }

            if (event.m_EventName.equalsIgnoreCase("Death")) {
                owner.setOnFloor(true);
                if (!GameServer.bServer) {
                    owner.Kill(owner.getAttackedBy());
                }
            }
        } else {
            DebugLog.log("PlayerHitReactionState.animEvent (" + event.m_EventName + ") zombie is null");
        }
    }
}
